package com.mentaleye.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.dal.dao.StudentAttendanceRecordDao;
import com.mentaleye.dal.model.StudentAttendanceRecord;
import com.mentaleye.dal.model.StudentAttendanceRecordExample;
import com.mentaleye.dal.model.StudentAttendanceRecordExample.Criteria;
import com.mentaleye.dal.model.StudentDisciplineRecord;
import com.mentaleye.service.StudentAttendanceRecordService;

@Service
public class StudentAttendanceRecordServiceImpl implements StudentAttendanceRecordService {

	@Autowired 
	private StudentAttendanceRecordDao recordDao;

	@Override
	public boolean insertByList(List<StudentAttendanceRecord> recordList) {
		return this.recordDao.insertByList(recordList);
	}

	@Override
	public boolean updateRecord(StudentAttendanceRecord record) {
		return this.recordDao.updateRecord(record);
	}

	@Override
	public boolean updateRecordSelective(StudentAttendanceRecord record) {
		StudentAttendanceRecordExample example=new StudentAttendanceRecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andStudentIdEqualTo(record.getStudentId());
		criteria.andWriteTimeEqualTo(record.getWriteTime());
		return this.recordDao.updateRecordBySelective(record, example);
	}
	
	@Override
	public StudentAttendanceRecord getBySelective(int studentId, Date writeTime) {
		StudentAttendanceRecord record=null;
		StudentAttendanceRecordExample example=new StudentAttendanceRecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andStudentIdEqualTo(studentId);
		criteria.andWriteTimeEqualTo(writeTime);
		List<StudentAttendanceRecord> recordList = this.recordDao.getListByExample(example);
		if(!recordList.isEmpty()){
			record=recordList.get(0);
		}
		return record;
	}
	
	@Override
	public List<StudentAttendanceRecord> getByDisciplineRecordList(List<StudentDisciplineRecord> disciplineRecordList) {
		List<Integer> studentIdList=new ArrayList<Integer>();
		List<Date> writeTimeList=new ArrayList<Date>();
    	for (StudentDisciplineRecord disciplineRecord : disciplineRecordList) {
    		studentIdList.add(disciplineRecord.getStudentId());
    		writeTimeList.add(disciplineRecord.getWriteTime());
		}
		
		StudentAttendanceRecordExample example=new StudentAttendanceRecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andStudentIdIn(studentIdList);
		criteria.andWriteTimeIn(writeTimeList);
		return this.recordDao.getListByExample(example);
	}
	
	@Override
	public List<StudentAttendanceRecord> getByDisciplineRecordListBySelective(List<Integer> studentIdList,
			List<Date> writeTimeList) {
		StudentAttendanceRecordExample example=new StudentAttendanceRecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andStudentIdIn(studentIdList);
		criteria.andWriteTimeIn(writeTimeList);
		return this.recordDao.getListByExample(example);
	}
	
	@Override
	public List<Map<String,Object>> getListByMap(Map<String, Object> map) {
		return this.recordDao.getListByMap(map);
	}

	@Override
	public boolean deleteByRecordId(long recordId) {
		StudentAttendanceRecordExample example=new StudentAttendanceRecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andRecordIdEqualTo(recordId);
		return this.recordDao.deleteByExample(example);
	}

	@Override
	public boolean deleteByRecordIdList(List<Long> recordIdList) {
		StudentAttendanceRecordExample example=new StudentAttendanceRecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andRecordIdIn(recordIdList);
		return this.recordDao.deleteByExample(example);
	}
	
	@Override
	public boolean deleteByWriteTime(Date writeTime) {
		StudentAttendanceRecordExample example=new StudentAttendanceRecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andWriteTimeEqualTo(writeTime);
		return this.recordDao.deleteByExample(example);
	}

	@Override
	public boolean deleteByDisciplineRecordList(List<StudentDisciplineRecord> disciplineRecordList) {
		List<Integer> studentIdList=new ArrayList<Integer>();
		List<Date> writeTimeList=new ArrayList<Date>();
    	for (StudentDisciplineRecord disciplineRecord : disciplineRecordList) {
    		studentIdList.add(disciplineRecord.getStudentId());
    		writeTimeList.add(disciplineRecord.getWriteTime());
		}
		
		StudentAttendanceRecordExample example=new StudentAttendanceRecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andStudentIdIn(studentIdList);
		criteria.andWriteTimeIn(writeTimeList);
		
		return this.recordDao.deleteByExample(example);
	}
}
